package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.Permission;
import com.erpanderp.wam.adm.model.entity.RolePermission;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class RolePermissionDAO {

	public List<RolePermission> getAllRolePermissions()
	{
		List<RolePermission> rolePermissions = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			String queryString = "from RolePermission m";
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			rolePermissions = (List<RolePermission>) query.list();	
			session.close();
			return rolePermissions;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public List<RolePermission> getAllRolePermissionByRoleId(Long roleId)
	{
		List<RolePermission> rolePermissions = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			String queryString = "from RolePermission rp where rp.role.roleId = :roleId";
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setLong("roleId", roleId);
			rolePermissions = (List<RolePermission>) query.list();	
			session.close();
			return rolePermissions;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public RolePermission createRolePermission(RolePermission rolePermission){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(rolePermission);
		session.getTransaction().commit();
		
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		return rolePermission;
		
	}
	
	public RolePermission updateRolePermission(RolePermission rolePermission){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.setFlushMode(FlushMode.COMMIT);
		session.update(rolePermission);
		session.getTransaction().commit();
		
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
		return rolePermission;		
	}

	public boolean hasRolePermission(String permission, Long employeeId){
		String permissionYes = CommonLookup.PERMISSION_YES;
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.setFlushMode(FlushMode.COMMIT);
		String queryString = "from RolePermission rp where rp.permission.permission =:permission and rp.permitted =:permissionYes and rp.role.roleId in (select er.role.roleId from EmployeeRole er where er.employee.employeeId = :employeeId)";
		Query query = session.createQuery(queryString);
		query.setCacheable(false).setString("permission", permission).setLong("employeeId", employeeId).setString("permissionYes", permissionYes);
		List temp = query.list();
		session.getTransaction().commit();
		
		if(temp != null && temp.size() > 0){
			return true;
		}else{
			return false;
		}
		
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}	
				
	}	
	
	public static void main(String a[]){
		System.out.println(new RolePermissionDAO().hasRolePermission(CommonLookup.PERMISSION_EDIT_EMPLOYEE_INFO, 2l));
	}
}
